package com.woniu.mgr.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniu.mgr.common.Result;
import com.woniu.mgr.controller.form.AccountModifyForm;
import com.woniu.mgr.model.Account;
import com.woniu.mgr.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.jdbc.Null;
import org.apache.naming.factory.ResourceLinkFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户账号表（学员和教练） 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/account")
@Slf4j
@Api(tags = "学员删改查")
public class AccountController {

    @Resource
    private AccountService accountService;

    @GetMapping("/accountAll")
    @ApiOperation("平台分页查询学员")
    public Result accountAll(Integer page, Integer size) {
        log.info("执行了分页查询学员 {}{}", page, size);
        IPage<Account> list = accountService.list(page, size);
        return Result.success(200, "查询成功", list);
    }

    @PutMapping("/accountModify")
    @ApiOperation("平台修改学员")
    public Result accountModify(AccountModifyForm accountModifyForm) {
        log.info("执行了学员修改操作 {}", accountModifyForm);
        accountService.accountModify(accountModifyForm);
        return Result.success(200, "修改成功", null);
    }

    @PostMapping("/accountDelete")
    public Result accountDelete(Integer id) {
        log.info("执行了禁用学员操作 {}", id);
        Account byId = accountService.getById(id);
        byId.setState("不可用");
        accountService.updateById(byId);
        return Result.success(200, "禁用成功", null);
    }

    @PostMapping("/accountStart")
    public Result accountStart(Integer id){
        log.info("执行了启用学员操作 {}", id);
        Account byId = accountService.getById(id);
        byId.setState("可用");
        accountService.updateById(byId);
        return Result.success(200, "启用成功", null);
    }




    @GetMapping("/findUser")
    @ApiOperation("平台分页查询学员")
    public Result findUser(String condition, Integer page, Integer size) {
        log.info("执行了分页查询学员 {}{}", page, size);
        IPage<Account> list = accountService.listUser(condition, page, size);
        return Result.success(200, "查询成功", list);
    }

    @GetMapping("/getOne")
    public Result getOne(Integer id) {
        Account byId = accountService.getById(id);
        return Result.success(200, "查询成功", byId);
    }

    @PostMapping("/del")
    public Result del(Integer id) {
        Account byId = accountService.getById(id);
        byId.setState("不可用");
        accountService.updateById(byId);
        return Result.success(200, "禁用成功", null);
    }

    @PostMapping("/start")
    public Result start(Integer id){
        log.info("执行了启用操作 {}",id);
        Account byId = accountService.getById(id);
        byId.setState("可用");
        accountService.updateById(byId);
        return Result.success(200, "启用成功", null);
    }
}

